package com.fer.hr.nmbp.lab2.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.ibatis.session.SqlSession;

import com.hr.fer.nmbp.lab2.domain.Guest;
import com.hr.fer.nmbp.lab2.domain.Reservation;
import com.hr.fer.nmbp.lab2.domain.Room;
import com.hr.fer.nmbp.lab2.persistence.GuestMapper;
import com.hr.fer.nmbp.lab2.persistence.ReservationMapper;
import com.hr.fer.nmbp.lab2.persistence.RoomMapper;
import com.toedter.calendar.JDateChooser;

public class AddReservationWindow extends JFrame {
	private static final long serialVersionUID = -603264146604505996L;
	
	private JPanel contentPane;
	private JTextField tbName;
	private JTextField tbSurname;
	private JTextField tbJMBG;
	private JComboBox comboBox;
	private DefaultComboBoxModel model;
	private List<Room> rooms;
	JDateChooser dtFirst;
	JDateChooser dtLast;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddReservationWindow frame = new AddReservationWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void showListRooms(){
		model.removeAllElements();
		SqlSession session = MainWindow.sessionFactory.openSession();
		RoomMapper mapper = session.getMapper(RoomMapper.class);
		
		rooms = mapper.selectByExample(null);
		
		for(int i=0;i<rooms.size();i++){
			model.insertElementAt(rooms.get(i).toString(),i);
		}
		
		session.commit();
		session.close();
	}
	
	private void closeClick(){
		this.dispose();
	}
	
	private void saveClick(){
		SqlSession session = null;
		session = MainWindow.sessionFactory.openSession();
		
		Guest guest = new Guest(tbJMBG.getText(),tbName.getText(),tbSurname.getText());
		GuestMapper mapper = session.getMapper(GuestMapper.class);
		mapper.insert(guest);
		Room room = rooms.get(comboBox.getSelectedIndex());
		
		Reservation reservation = new Reservation(dtFirst.getDate(),dtLast.getDate(),guest,room);
		
		ReservationMapper reservationMapper = session.getMapper(ReservationMapper.class);
		reservationMapper.insert(reservation);
		
		session.commit();
		
		session.close();
		
		closeClick();
	}
	/**
	 * Create the frame.
	 */
	public AddReservationWindow() {
		setTitle("Dodaj rezervaciju");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 298, 290);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Ime:");
		lblNewLabel.setBounds(10, 24, 46, 14);
		contentPane.add(lblNewLabel);
		
		JLabel lblPrezime = new JLabel("Prezime:");
		lblPrezime.setBounds(10, 49, 62, 14);
		contentPane.add(lblPrezime);
		
		JLabel lblJmbg = new JLabel("JMBG:");
		lblJmbg.setBounds(10, 74, 46, 14);
		contentPane.add(lblJmbg);
		
		JLabel lblSoba = new JLabel("Soba:");
		lblSoba.setBounds(10, 99, 46, 14);
		contentPane.add(lblSoba);
		
		JLabel lblPocetak = new JLabel("Prvi dan:");
		lblPocetak.setBounds(10, 134, 62, 14);
		contentPane.add(lblPocetak);
		
		JLabel lblZadnji = new JLabel("Zadnji dan:");
		lblZadnji.setBounds(10, 164, 75, 14);
		contentPane.add(lblZadnji);
		
		tbName = new JTextField();
		tbName.setBounds(109, 21, 163, 20);
		contentPane.add(tbName);
		tbName.setColumns(10);
		
		tbSurname = new JTextField();
		tbSurname.setBounds(109, 46, 163, 20);
		contentPane.add(tbSurname);
		tbSurname.setColumns(10);
		
		tbJMBG = new JTextField();
		tbJMBG.setBounds(109, 71, 163, 20);
		contentPane.add(tbJMBG);
		tbJMBG.setColumns(10);
		
		dtFirst = new JDateChooser();
		dtFirst.setBounds(109, 127, 163, 20);
		contentPane.add(dtFirst);
		
		dtLast = new JDateChooser();
		dtLast.setBounds(109, 158, 163, 20);
		contentPane.add(dtLast);
		
		JButton btnOdustani = new JButton("Odustani");
		btnOdustani.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				closeClick();
			}
		});
		btnOdustani.setBounds(10, 205, 89, 23);
		contentPane.add(btnOdustani);
		
		JButton btnSpremi = new JButton("Spremi");
		btnSpremi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveClick();
			}
		});
		btnSpremi.setBounds(183, 205, 89, 23);
		contentPane.add(btnSpremi);
		
		model = new DefaultComboBoxModel();
		comboBox = new JComboBox(model);
		comboBox.setBounds(109, 96, 163, 20);
		contentPane.add(comboBox);
		showListRooms();
	}
}
